<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2015 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Friendlink.php 02603 2018-10-10 16:47:33 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：对应的表名:tky_friendlink
 * +----------------------------------------------------------------------
 */

namespace application\admin\controller;

use ticky\request;

class friendlink extends base {

    //首页
    public function index() {
        $ret = db('friendlink')->order('displayorder')->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->assign('search', $search['arr']);
        $this->display('friendlink/index');
    }

    // 批量删除
    public function batchremove() {
        $optype = request::post('optype', '');
        $ids = request::post('ids', []);
        if ($optype == "del") {
            for ($i = 0; $i < count($ids); $i++) {
                $id = $ids[$i];
                db('friendlink')->delete(['id' => $id]);
            }
            showmsg('删除成功', '/admin/friendlink');
        } else {
            if (is_array($_GET['name'])) {
                foreach ($_GET['name'] as $id => $val) {
                    $type_str = intval($_GET['portal'][$id]) . intval($_GET['forum'][$id]) . intval($_GET['group'][$id]) . intval($_GET['home'][$id]);
                    $type_str = intval($type_str, '2');

                    $arr = array();
                    $arr['displayorder'] = $_GET['displayorder'][$id];
                    $arr['name'] = $_GET['name'][$id];
                    $arr['url'] = $_GET['url'][$id];
                    $arr['description'] = $_GET['description'][$id];
                    $arr['logo'] = $_GET['logo'][$id];
                    $arr['type'] = $type_str;

                    db('friendlink')->where(['id' => $id])->update($arr);
                }
            }

            if (is_array($_GET['newname'])) {
                foreach ($_GET['newname'] as $key => $value) {
                    if ($value) {
                        $type_str = intval($_GET['newportal'][$key]) . intval($_GET['newforum'][$key]) . intval($_GET['newgroup'][$key]) . intval($_GET['newhome'][$key]);
                        $type_str = intval($type_str, '2');

                        $arr = array();
                        $arr['displayorder'] = $_GET['newdisplayorder'][$key];
                        $arr['name'] = $value;
                        $arr['url'] = $_GET['newurl'][$key];
                        $arr['description'] = $_GET['newdescription'][$key];
                        $arr['logo'] = $_GET['newlogo'][$key];
                        $arr['type'] = $type_str;

                        db('friendlink')->insert($arr);
                    }
                }
            }

            showmsg('更新成功', '/admin/friendlink');
        }
    }

}
